﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;


namespace Grizzy_X
{
    public class CBaseMap
    {
        public int ID;
        public string Name;
        //Unit:pixel
        public int x;
        public int y;
        public int Width;
        public int Height;
        public string FilePath;
        public Bitmap basebmp;
        public bool Visible;
        //public string unit;
        public void New(string n)
        {
            //Min ID=1
            ID = Controller.CountBase;
            Name = n;
            Visible = true;
        }

        public Bitmap DrawBase()
        {
            try
            {
                Initial();
                Image basemap = Image.FromFile(FilePath);
                basebmp = new Bitmap(Controller.View_Width, Controller.View_Height);
                Graphics a = Graphics.FromImage(basebmp);
                a.DrawImage(basemap, x, y, Width, Height);
                a.Dispose();
                return basebmp;
            }
            catch
            {
                return null;
            }
        }

        //默认在初始状态下导入底图
        //记录左上点的真实坐标,以及右下。。。
        PointF Left_top;
        PointF Right_bottom;

        private void Initial()
        {
            Left_top = Transformer.FromScreen(new Point(x, y));
            Right_bottom = Transformer.FromScreen(new Point((x + Width), (y+Height)));
        }

        
        public Bitmap DrawChange()
        {
            try
            {
                //计算起始像素
                Point startpixel=new Point();
                Image basemap = Image.FromFile(FilePath);
                startpixel.X=(int)Math.Round((Transformer.OffsetX - Left_top.X) / (Right_bottom.X - Left_top.X) * basemap.Width);
                startpixel.Y =(int)Math.Round((Left_top.Y-Transformer.OffsetY) / (Left_top.Y - Right_bottom.Y) * basemap.Height);
                GraphicsUnit units = GraphicsUnit.Pixel;
                /*double scalex = Convert.ToDouble(Width) / Convert.ToDouble(basemap.Width);
                double scaley = Convert.ToDouble(Height) / Convert.ToDouble(basemap.Height);
                int tempX = (int)(startpixel.X / scalex);
                int tempY = (int)(startpixel.Y / scaley);
                startpixel.X = tempX;
                startpixel.Y = tempY;*/
                Rectangle srcRect = new Rectangle(startpixel.X, startpixel.Y, (int)Math.Round((basemap.Width/ Transformer.Displayscale)*(Convert.ToDouble(Controller.View_Width)/Convert.ToDouble(Width))),(int)Math.Round((basemap.Height/ Transformer.Displayscale)*(Convert.ToDouble(Controller.View_Height)/Convert.ToDouble(Height))));
                basebmp = new Bitmap(Controller.View_Width, Controller.View_Height);
                Graphics a = Graphics.FromImage(basebmp);
                
                a.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                a.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                a.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                a.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
                Rectangle destRec=new Rectangle(0,0,Controller.View_Width,Controller.View_Height);
                a.DrawImage(basemap, destRec, srcRect, units);
                a.Dispose();
                return basebmp;
            }
            catch
            {
                return null;
            }
        }

    }
}
